<?php

namespace app\admin\model;

use think\model\Relation;

class Tours extends Relation {

        protected $fields = ['id', 'code', 'pretitle', 'title', 'subtitle', 'uid', 'cid', 'theme_id', 'traffic', 'sight_id', 'sight_cache', 'attachpath', 'attachment', 'attachthumb', 'feature', 'notice', 'dateline', 'travelcompany', 'travelurl', 'supplier_id', 'hits', 'isgood', 'istop', 'ispreference', 'isindex', 'ischecked', 'isdelete', 'isnotshow', 'seo_title', 'seo_keywords', 'seo_description','lead_time', 'leave_time', 'trip_days', 'price', 'traffic_id', 'labels', 'type', 'from_zone', 'img', 'tag', 'dingdan', 'manyi', 'guanzhu','score_jiaotong','score_canyin','score_chuyou','score_kefu','score_daoyou','score_zhusu'];
        protected $pk = 'id';
        protected $link = [
            'member' => [
                'mapping_type' => BELONGS_TO,
                'class_name' => 'Member',
                'foreign_key' => 'uid',
                'mapping_fields' => 'realname as username',
                'as_fields' => 'username',
                'parent_key' => 'id'
            ],
            'price' => [
                'mapping_type' => HAS_MANY,
                'class_name' => 'ToursPrice',
                'foreign_key' => 'tours_id',
                'parent_key' => 'id',
                'mapping_order' => "id asc",
                'condition' => "case_id = 0"
            ],
            'all_price' => [
                'mapping_type' => HAS_MANY,
                'class_name' => 'ToursPrice',
                'foreign_key' => 'tours_id',
                'parent_key' => 'id',
                'mapping_order' => "id asc",
            ],
            'trip' => [
                'mapping_type' => HAS_MANY,
                'class_name' => 'ToursTrip',
                'foreign_key' => 'tours_id',
                'parent_key' => 'id',
                'mapping_order' => "day asc"
            ],
            'case' => [
                'mapping_type' => HAS_MANY,
                'class_name' => 'ToursCase',
                'foreign_key' => 'tours_id',
                'parent_key' => 'id'
            ]
            
        ];

        protected function _after_select(&$resultSet, $options = array()) {
                parent::_after_select($resultSet, $options);
                foreach ($resultSet as $key => $value) {
                        $resultSet[$key]['url'] = U("/xianlu/" . $value['id'], '', 'html', 'www');
                        $resultSet[$key]['failure'] = true;
                        foreach ($value['all_price'] as $v) {
                                switch ($v['price_type']) {
                                        case 1:
                                        case 2:
                                        case 3:
                                                if ($v['end_time'] >= date("Y-m-d")) {
                                                        $resultSet[$key]['failure'] = false;
                                                        break 2;
                                                }
                                                break;
                                        case 4:
                                                if ($v['start_time'] >= date("Y-m-d")) {
                                                        $resultSet[$key]['failure'] = false;
                                                        break 2;
                                                }
                                                break;
                                }
                        }
                }
        }

        protected function _after_find(&$result, $options = array()) {
                parent::_after_find($result, $options);
                if (!empty($result['cid'])) {
                        $cid = explode(",", trim($result['cid'], ","));
                        $result['cid'] = $result['cid'];
                } else {
                        $cid = array();
                        $result['cid'] = '';
                }
                $pt_ids = M('Location')->where(['pid' => ['in', '1,2']])->field('id')->select();
                $vv = '';
                foreach ($pt_ids as $v) {
                        $vv = $vv . ',' . $v['id'];
                }
                $arr_nd = explode(',', $vv);
                foreach ($cid as $k => $v) {
                        //过滤最高级
                        if ($v == 1 || $v == 2 || in_array($v, $arr_nd)) {
                                continue;
                        } else {
                                $list = M("ToursLabel")->where(array("prov_id" => array("like", "%," . $v . ",%")))->select();
                        }
                        foreach ($list as $k => $val) {
                                if (!empty($list)) {
                                        $labels[$val['id']] = $val;
                                        $labels_id[] = $val['id'];
                                }
                        }
                }

                $result['label_html'] = '';
                if (!empty($labels)) {
                        foreach ($labels as $k => $v) {
                                $checked = "";
                                if (in_array($v['id'], explode(",", trim($result['labels'], ",")))) {
                                        $checked = "checked";
                                }
                                $result['label_html'] .= "<label style='width:150px;'><input type='checkbox' data-toggle='icheck'  $checked name='labels[]' value='" . $v['id'] . "' data-label='" . $v['title'] . "'>" . "</label>";
                        }
                }
                if (!empty($result['price'])) {
                        $result['price'] = $result['price'];
                } else {
                        $result['price'] = array();
                }
                if (!empty($result['trip'])) {
                        $result['trip'] = $result['trip'];
                } else {
                        $result['trip'] = array();
                }
                if (!empty($result['case'])) {
                        $result['case'] = $result['case'];
                } else {
                        $result['case'] = array();
                }
                foreach ($result['price'] as $key => $value) {
                        $result['price'][$key]['price_value'] = unserialize($value['price_value']);
                }
                foreach ($result['trip'] as $key => $value) {
                        $result['trip'][$key]['images'] = unserialize($value['images']);
                }
                foreach ($result['case'] as $key => $value) {
                        $case_price = M("ToursPrice")->where(array('tours_id' => $result['id'], 'case_id' => $value['id']))->order("id asc")->select();
                        $price = array();
                        foreach ($case_price as $v) {
                                $data = unserialize($v['price_value']);
                                $data['type'] = $v['price_type'];
                                $price[] = json_encode($data);
                        }
                        $result['case'][$key]['price'] = $price;
                }

                $catename = M('Location')->field('name')->order("id desc")->where(array('id' => array('in', $result['cid'])))->select();
                $arrprocess = new \org\Arrayprocess();
                $result['cate'] = str_replace("'", "", $arrprocess::array_to_string($catename, "name"));
        }

}
